package com.ncloud.crm.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.ncloud.crm.entity.BsContractNode;
@Mapper
public interface BsContractNodeMapper {

	@Insert("insert into bs_node_contract_rel(contract_id,node_id,begin_date,end_date,update_time,update_user,remark)"
			+ " values(#{contrInstId},#{nodeId},#{beginDate},#{endDate},#{updateTime},#{updateUser},#{remark})")
	int createContractNodeRel(BsContractNode contractNode);

	@Select("select a.contr_inst_id,a.node_id,a.begin_date,a.end_date,a.update_time,a.update_user"
			+ " from bs_node_contract_rel a,bs_contract b where now() between a.begin_date and a.end_date "
			+ " and a.contr_inst_id = b.child_contract_inst_id and b.state = '1' "
			+ " and (a.contr_inst_id = #{contractInstId} or #{contractInstId} is null)")
	@Results({@Result(column="contr_inst_id",property="contrInstId"),@Result(column="node_id",property="nodeId"),
		@Result(column="begin_date",property="beginDate"),@Result(column="end_date",property="endDate"),
		@Result(column="update_time",property="updateTime"),@Result(column="update_user",property="updateUser")})
	List<BsContractNode> getNodesByContract(@Param("contractInstId")Long contractInstId);

}
